﻿Imports System.Data.OleDb


Public Class Detail
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim id As Integer = Request.QueryString("ID")

        Dim sql As String = "select * from cars where ID =" & id
        Dim sql1 As String = "select * from likes where cid=" & id & " and reader=" & User.Identity().Name
        Dim sql2 As String = "select count(*) from likes where cid=" & id

        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        conn.Open()

        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        Using Reader As OleDbDataReader = cmd.ExecuteReader()
            If Reader.Read() Then
                
                lbl_Model.Text = Reader("cModel")
                lbl_name.Text = Reader("cName")
                lbl_engine.Text = Reader("Engine")
                lbl_speed.Text = Reader("Topspeed")
                lbl_frame.Text = Reader("Frame")
                lbl_body.Text = Reader("Body")
                lbl_suspension.Text = Reader("Suspension")
                lbl_brakes.Text = Reader("Brakes")
                lbl_stering.Text = Reader("Stering")
                lbl_tires.Text = Reader("Tires")
                Image4.ImageUrl = Reader("cImage")


            End If
        End Using

        Dim cmd2 As OleDbCommand = New OleDbCommand(sql2, conn)
        'Dim drr As OleDbDataReader = cmd2.ExecuteReader()


        Dim sum = cmd2.ExecuteScalar()

        users.Text = (sum - 1)


        Dim cmd1 As OleDbCommand = New OleDbCommand(sql1, conn)
        cmd1.Parameters.AddWithValue("@f1", User.Identity().Name)
        Using dr As OleDbDataReader = cmd1.ExecuteReader()

            If dr.HasRows() Then

                ImageButton1.Visible = False
                you.Visible = True
                users.Visible = True
                likesthis.Visible = True
                ImageButton2.Visible = True

            End If
        End Using
        conn.Close()



    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

        Dim sql As String = "INSERT INTO comments (Comment,cID,uID) VALUES (@f1,@f2,@f3)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", comment.Text)
        cmd.Parameters.AddWithValue("@f2", Request.QueryString("ID"))
        cmd.Parameters.AddWithValue("@f3", User.Identity().Name)
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()

        Response.Redirect("Details.aspx?ID=" & Request.QueryString("ID"))
       

    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click

        If newtag.Text = "" Then

            Dim sql As String = "INSERT INTO cartags (cars_fkid,tag_fkid) VALUES (@f1,@f2)"
            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)

            conn.Open()

            For Each cb As ListItem In tag.Items
                If cb.Selected = True Then

                    Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
                    cmd.Parameters.AddWithValue("@f1", Request.QueryString("ID"))
                    cmd.Parameters.AddWithValue("@f2", cb.Value)
                    cmd.ExecuteNonQuery()

                End If
            Next

            conn.Close()
            Response.Redirect("Details.aspx?ID=" & Request.QueryString("ID"))
        Else

            Dim sql As String = "INSERT INTO cartags (cars_fkid,tag_fkid) VALUES (@f1,@f2)"
            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            conn.Open()

            For Each cb As ListItem In tag.Items
                If cb.Selected = True Then

                    Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
                    cmd.Parameters.AddWithValue("@f1", Request.QueryString("ID"))
                    cmd.Parameters.AddWithValue("@f2", cb.Value)
                    cmd.ExecuteNonQuery()

                End If
            Next

            Dim sql1 As String = "INSERT INTO tag (TagName) VALUES (@f1)"
            Dim cmd1 As OleDbCommand = New OleDbCommand(sql1, conn)
            cmd1.Parameters.AddWithValue("@f1", newtag.Text)
            cmd1.ExecuteNonQuery()

            Dim sql2 As String = "select top 1 ID from tag ORDER BY ID desc"
            Dim cmd2 As OleDbCommand = New OleDbCommand(sql2, conn)
            Dim dr As OleDbDataReader = cmd2.ExecuteReader()
            dr.Read()
            Dim topID = dr(0)

            Dim sql3 As String = "INSERT INTO cartags (cars_fkid,tag_fkid) VALUES (@f1,@f2)"
            Dim cmd3 As OleDbCommand = New OleDbCommand(sql3, conn)
            cmd3.Parameters.AddWithValue("@f1", Request.QueryString("ID"))
            cmd3.Parameters.AddWithValue("@f2", topID)
            cmd3.ExecuteNonQuery()

            conn.Close()

            Response.Redirect("Details.aspx?ID=" & Request.QueryString("ID"))

        End If
    End Sub

    Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click

        Dim sql As String = "INSERT INTO likes (reader,cid) VALUES (@f1,@f2)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        cmd.Parameters.AddWithValue("@f1", User.Identity().Name)
        cmd.Parameters.AddWithValue("@f2", Request.QueryString("ID"))

        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()

        Response.Redirect("Details.aspx?ID=" & Request.Params("ID"))


    End Sub
End Class